#include <stdio.h>

char * myfunc(int a)
{
    static char x[7][16] = {"Monday", "Tuesday", "Wednesday", 
                            "Thursday", "Friday", "Saturday", "Sunday"};

    return &x[a-1][0];
}

int main()
{
    printf("%s\n", myfunc(6));   // Saturday

	return 0;
}

#if 0

/*
 * intel
 */
0000000000001149 <myfunc>:
    1149:	f3 0f 1e fa          	endbr64 
    114d:	55                   	push   %rbp
    114e:	48 89 e5             	mov    %rsp,%rbp
    1151:	89 7d fc             	mov    %edi,-0x4(%rbp)	// rbp-0x4=edi
    1154:	8b 45 fc             	mov    -0x4(%rbp),%eax	// eax=rbp-0x4
    1157:	83 e8 01             	sub    $0x1,%eax	// eax-=1
    115a:	48 98                	cltq   
    115c:	48 c1 e0 04          	shl    $0x4,%rax	// rax<<=4
    1160:	48 89 c2             	mov    %rax,%rdx	// rdx=rax
    1163:	48 8d 05 b6 2e 00 00 	lea    0x2eb6(%rip),%rax        # 4020 <x.2316> rax=rip+0x2eb6
    116a:	48 01 d0             	add    %rdx,%rax	// rax+=rdx
    116d:	5d                   	pop    %rbp
    116e:	c3                   	retq   

000000000000116f <main>:
    116f:	f3 0f 1e fa          	endbr64 
    1173:	55                   	push   %rbp		
    1174:	48 89 e5             	mov    %rsp,%rbp	// rbp=rsp
    1177:	bf 06 00 00 00       	mov    $0x6,%edi	// edi=0x6
    117c:	e8 c8 ff ff ff       	callq  1149 <myfunc>	// myfunc(edi)
    1181:	48 89 c7             	mov    %rax,%rdi	// rdi=rax
    1184:	e8 c7 fe ff ff       	callq  1050 <puts@plt>	// puts(rdi)
    1189:	b8 00 00 00 00       	mov    $0x0,%eax	// eax=0
    118e:	5d                   	pop    %rbp
    118f:	c3                   	retq   

/*
 * arm
 */
000000000040055c <myfunc>:
  40055c:	d10043ff 	sub	sp, sp, #0x10
  400560:	b9000fe0 	str	w0, [sp, #12]		// [sp+12]=w0
  400564:	b9400fe0 	ldr	w0, [sp, #12]		// w0=[sp+12]
  400568:	51000400 	sub	w0, w0, #0x1		// w0=w0-1
  40056c:	93407c00 	sxtw	x0, w0			// x0=w0
  400570:	d37cec01 	lsl	x1, x0, #4		// x1=x0<<4
  400574:	b0000080 	adrp	x0, 411000 <__libc_start_main@GLIBC_2.17>
  400578:	9100c000 	add	x0, x0, #0x30		// x0=411030
  40057c:	8b000020 	add	x0, x1, x0		// x0=x1+x0
  400580:	910043ff 	add	sp, sp, #0x10
  400584:	d65f03c0 	ret

0000000000400588 <main>:
  400588:	a9bf7bfd 	stp	x29, x30, [sp, #-16]!
  40058c:	910003fd 	mov	x29, sp
  400590:	528000c0 	mov	w0, #0x6                   	// #6
  400594:	97fffff2 	bl	40055c <myfunc>			// myfunc(w0)
  400598:	97ffffae 	bl	400450 <puts@plt>		// puts(w0)
  40059c:	52800000 	mov	w0, #0x0                   	// #0
  4005a0:	a8c17bfd 	ldp	x29, x30, [sp], #16
  4005a4:	d65f03c0 	ret

/*
 * arm-v7
 */
char * myfunc(int a)
{
   0:	e52db004 	push	{fp}		; (str fp, [sp, #-4]!)
   4:	e28db000 	add	fp, sp, #0
   8:	e24dd00c 	sub	sp, sp, #12
   c:	e50b0008 	str	r0, [fp, #-8] ; a
    static char x[7][16] = {"Monday", "Tuesday", "Wednesday", 
                            "Thursday", "Friday", "Saturday", "Sunday"};

    return &x[a-1][0];
  10:	e51b3008 	ldr	r3, [fp, #-8] ; a
  14:	e2433001 	sub	r3, r3, #1    ; a-1
  18:	e1a03203 	lsl	r3, r3, #4    ; 在这里[a-1]与16相乘。左移4位等价于乘以2的4次幂或者16
  1c:	e59f2010 	ldr	r2, [pc, #16]	; 34 <myfunc+0x34>
  20:	e0833002 	add	r3, r3, r2      ; 将所获得的值与引用数据段中字符串表的基地址相加。数据段包含静态或者全局变量。由于参数a的值与某个值相乘，因此可以假想这是一个固定长度的两维字符串数据组。可见，r3包含一个指向索引编号为a-1或者a的字符串指针，假如说数组的索引值从零开始计数的话。
}
  24:	e1a00003 	mov	r0, r3  ; 指向数组相应元素的指针通过r3返回。正如所看到的一样，在返回指向堆中所分配的内存块的指针与返回指向在数据段中所分配的静态变量的指针之间，并不存在本质的不同。
  28:	e24bd000 	sub	sp, fp, #0
  2c:	e49db004 	pop	{fp}		; (ldr fp, [sp], #4)
  30:	e12fff1e 	bx	lr
  34:	00000000 	.word	0x00000000

00000038 <main>:

void main()
{
  38:	e92d4800 	push	{fp, lr}
  3c:	e28db004 	add	fp, sp, #4
    printf("%s\n", myfunc(6));
  40:	e3a00006 	mov	r0, #6
  44:	ebfffffe 	bl	0 <myfunc>
  48:	e1a03000 	mov	r3, r0
  4c:	e1a00003 	mov	r0, r3
  50:	ebfffffe 	bl	0 <puts>
}

#endif
